<template>
	<view class="recharge">
		<view class="header">
			<view class="left">
				<view class="img">
					<image src="../../static/images/img/qianbao1.png" mode="widthFix"></image>
				</view>
				<view class="info">
					<view class="text">可用</view>
					<view class="balance">
						<text>{{userMoney.toFixed(2)}}</text>余额(元)
					</view>
				</view>
			</view>
			<view class="right">账单</view>
		</view>

		<!-- 选择金额 -->
		<view class="amount">
			<view class="title">
				充值
			</view>
			<u-radio-group v-model="money" class="select">
				<u-radio active-color="#39b54a" v-for="(item, index) in list" :key="index" :name="item.name">
					{{item.name}}
				</u-radio>
			</u-radio-group>
		</view>

		<!-- 充值 -->
		<view class="cart">
			<view class="left" @tap="show = !show">
				<view class="price">￥<text>{{money}}</text></view>
			</view>
			<view class="right" @click="$refs.payment.show()">立即充值</view>
		</view>

		<!-- 键盘 -->
		<Payment ref="payment" :show="show" :money="money" @pay="pay" />

		<!-- 支付成功 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		addRecharge,
		getUserInfo
	} from '../../api/api.js'

	import Payment from '../../components/Payment.vue'

	export default {
		data() {
			return {
				list: [{
					name: 300,
				}, {
					name: 500,
				}, {
					name: 1000,
				}],
				show: false,
				money: 300,
				userMoney: 0
			}
		},
		methods: {
			async pay(pw) {
				const res = await addRecharge({
					money: this.money,
					password: pw
				})
				this.$refs.uToast.show({
					title: res.msg,
					type: 'success',
				})
				this.$refs.payment.hide()
				const userInfo = await getUserInfo()
				getApp().globalData.userInfo = userInfo.data
				this.userMoney = userInfo.data.money
			}
		},
		components: {
			Payment
		},
		onShow() {
			this.userMoney = getApp().globalData.userInfo.money
		}
	}
</script>

<style scoped lang="scss">
	.recharge {
		.header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 50upx 30upx;
			background-color: #fff;

			.left {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.img {
					width: 100upx;

					image {
						width: 100%;
					}
				}

				.info {
					margin-left: 10upx;

					text {
						font-weight: bold;
						font-size: 16px;
					}
				}
			}
		}

		.amount {
			.title {
				margin: 50upx 30upx;
			}

			.select {
				display: flex;
				justify-content: space-around;
				align-items: center;
			}
		}

		.cart {
			position: fixed;
			bottom: 20upx;
			width: 95%;
			height: 80upx;
			left: 50%;
			transform: translateX(-50%);
			border-radius: 40upx;
			z-index: 100;
			display: flex;
			justify-content: space-between;
			align-items: center;
			overflow: hidden;
			background-color: #3b3838;
			color: #fff;

			view {
				height: 100%;
				line-height: 80upx;
			}

			.left {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				margin-left: 30upx;

				.price {

					text {
						font-size: 18px;
					}
				}
			}

			.right {
				font-size: 13px;
				background-color: $themeColor;
				padding: 0 30upx;
			}
		}

	}
</style>
